package com.graphics;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * @Author: clm
 * @Date: 2018-10-12
 */

public class GLRender implements GLSurfaceView.Renderer {
    protected double startTime, currentTime, exTime;
    protected IGLLooper mIGLLooper;
    public interface IGLLooper{
        void init();
        void glLooper(double exTime);
    }
    //投影矩阵
    public float mP_matrix[] = new float[16];
    /**
     * 创建gl窗口时初始化工作
     * @param gl10
     * @param eglConfig
     */
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        startTime = System.currentTimeMillis();
        currentTime = startTime;
        GLES20.glClearColor(0.35f, 0.75f, 0.75f, 0.75f);
    }

    /**
     * 设置循环回调
     * @param iglLooper
     */
    public void setGLooper(IGLLooper iglLooper){
        mIGLLooper = iglLooper;
    }

    /**
     * gl客户区改变时
     * @param gl10
     * @param w
     * @param h
     */
    @Override
    public void onSurfaceChanged(GL10 gl10, int w, int h) {
        Matrix.perspectiveM(mP_matrix, 0, 45F, w / h, 0.1f, 1000f);
        GLES20.glViewport(0, 0, w, h);
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        exTime = System.currentTimeMillis() - currentTime;
        currentTime += exTime;
        if(mIGLLooper != null){
            mIGLLooper.glLooper(exTime);
        }
    }
}
